home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ8801.ZIP / CCDEMO.ZIP / CCDEMO.C next >
C/C++ Source or Header  |  1987-12-04  |  17KB  |  621 lines

  1. /*------------------------------ file information -----------------------------*/
  2.                                                                                                             
  3. /*         
  4.     custom controls demo.c    
  5.  
  6.          c source code file for a minimal Mac program that demonstrates 
  7.              controls drawn with a custom CDEF resource
  8.  
  9.          the custom CDEF resource that's demonstrated provides 16 button variations
  10.          the buttons ╔
  11.                  í ╔ live in a rectangular space
  12.                  í ╔ can be outlined, shadowed, or bare
  13.                  í ╔ can contain text in any font-style-size, an icon, or a picture
  14.                  í ╔ can indicate highlighting via inversion or a change of content
  15.      
  16.     edited and compiled with Lightspeed C 2.13
  17.     
  18.     written and ⌐1987 by Stan Krute. all rights reserved. no part of this file, 
  19.     or the object code it leads to, may be reproduced, in any form or by any means, 
  20.     without    the express written permission of the author and copyright holder. 
  21.     
  22.     timestamp:        3:49 pm PST                 November 16, 1987 
  23.     spacestamp:        18617 Camp Creek Road    Hornbrook, California   96044
  24.     
  25.      this file looks good in 9 point Courier, LSC tabs set to 3
  26. */
  27.  
  28.  
  29. /*--------------------------------- include files -----------------------------*/
  30.  
  31. /* definitions for Mac OS managers used herein */
  32. #include    "ControlMgr.h"
  33. #include    "DialogMgr.h"        
  34. #include    "EventMgr.h"    
  35. #include    "FontMgr.h"    
  36. #include    "MenuMgr.h"    
  37. #include    "Quickdraw.h"                    
  38. #include    "StdFilePkg.h"
  39.  
  40. /* our stuff */
  41. #include    "custom controls demo.h"        /*   private definitions for this file  */ 
  42.  
  43.  
  44. /*----------------------------- main program block ----------------------------*/
  45.  
  46. void    main()
  47.  
  48.         {
  49.         /* local variable */
  50.         int        theItem ;
  51.         
  52.         /* initialize Mac OS managers  */
  53.         initializeManagers() ;             
  54.         
  55.         /* see what the world is like */
  56.         studyAndSetEnvironment () ;
  57.         
  58.         /* set up and draw a (dummy) title menu */
  59.         InsertMenu( GetMenu(titleMenuID), append ) ;
  60.         DrawMenuBar() ;
  61.  
  62.         /* set up and draw a modal dialog window */
  63.         getThatDialogCookin () ;
  64.         
  65.         /* initalize our doneness indicator */
  66.         finished = false ;
  67.         
  68.         /*  run the main event loop   */
  69.         do                                                    
  70.             {
  71.             ModalDialog (noFilterProcedure, &theItem) ;
  72.             dealWithDialogItem (theItem) ;
  73.             }
  74.         while                                            
  75.             ( ! finished ) ;
  76.  
  77.         /*  leave neatly when done */
  78.         DisposDialog (ourDialog) ;            /* bye bye to dialog */
  79.         ExitToShell() ;                        /* bye bye to program */
  80.         }
  81.  
  82.  
  83. /*---------------------------- initializeManagers -----------------------------*/
  84.  
  85. /*   initialize the heap, cursor, and Mac Operating System managers    */
  86.  
  87. void    initializeManagers()
  88.         
  89.         {
  90.         /* local variable */
  91.         Handle        someDay ;
  92.         
  93.         /* get some space */
  94.         MoreMasters() ;                                    /* get some master pointers */
  95.         if (someDay = NewHandle(humungousBlock))    /* grow a maximal heap by */ 
  96.             DisposHandle (someDay) ;                     /* asking for the future */ 
  97.               
  98.         /* get those managers going */
  99.         InitGraf(&thePort) ;                        /* set up Quickdraw */
  100.         InitFonts();                                /* set up the Font Manager */
  101.         InitWindows();                                /* set up the Window Manager */
  102.         InitMenus();                                /* set up the Menu Manager */
  103.         TEInit();                                    /* set up Text Edit */
  104.         InitDialogs (noResumeProcedure) ;    /* set up the Dialog Manager */
  105.  
  106.         /* final adjustments */
  107.         FlushEvents (everyEvent, dontStop ) ;    /* clear the event queue */
  108.         InitCursor();                                    /* turn the cursor on */
  109.         }
  110.  
  111.  
  112. /*---------------------------- studyAndSetEnvironment -------------------------*/
  113.  
  114. /*   check out screens, machines, ROMs, et al */
  115.  
  116. void    studyAndSetEnvironment () 
  117.         
  118.         {
  119.         /* check out the screen */
  120.         screenRect = screenBits.bounds ;
  121.         screenHeight = screenRect.bottom - screenRect.top ;
  122.         screenWidth = screenRect.right - screenRect.left ; 
  123.         
  124.         /* determine height of the menu bar */
  125.         if     ( ROM85 & 0x8000 )
  126.             menuBarHeight = stdMBarHeight ;      /* for 64K ROMs    */
  127.         else
  128.             menuBarHeight = MBarHeight ;            /* for newer ROMs    */
  129.         }
  130.  
  131.  
  132. /*------------------------------- getThatDialogCookin -------------------------*/
  133.  
  134. /* set up and draw our main modal dialog window */
  135.  
  136. void    getThatDialogCookin ()
  137.  
  138.         {
  139.         /* local variables */
  140.         Point                tempPoint ;
  141.         Rect                scratch ;
  142.         ControlHandle    theButton ;
  143.         
  144.         /* get the dialog window */
  145.         ourDialog = GetNewDialog (ourDialogID, storeInHeap, inFront) ;
  146.         
  147.         /* adjust its position  */
  148.         MoveWindow ( ourDialog, 
  149.                        (tempPoint = figureCenteredRectTLC (&(*ourDialog).portRect)).h,
  150.                             tempPoint.v, inFront ) ;
  151.         
  152.         /* make dialog window the current grafPort so we can change its font */
  153.         SetPort (ourDialog) ;
  154.         
  155.         /* change its font to Geneva 12 */
  156.         TextFont (geneva) ;
  157.         TextSize (12) ;
  158.         
  159.         /* show the dialog */
  160.         ShowWindow (ourDialog ) ;
  161.         }
  162.  
  163.  
  164. /*-------------------------------- dealWithDialogItem -------------------------*/
  165.  
  166.  /* deal with the hit item */  
  167.  
  168. void    dealWithDialogItem (theItem) 
  169.         int        theItem ;
  170.         
  171.         {
  172.         /* local constants */
  173.         #define        oolSize        6
  174.         
  175.         /* local variables */
  176.         static    short    onOffList[oolSize] = {     orwellItem, hupCoupleItem, 
  177.                                                             ronItem, saveAsItem, 
  178.                                                             pinheadItem, duplicateItem} ; 
  179.         /* case out on the item */
  180.         switch (theItem)
  181.             {
  182.             case quitItem:
  183.                 finished = true ;
  184.                 break ;
  185.             case orwellItem:
  186.                 doOrwellItem () ;
  187.                 break ;
  188.             case snapshotItem:
  189.                 doSnapshotItem () ;
  190.                 break ;
  191.             case mushroomItem:
  192.                 doMushroomItem () ;
  193.                 break ;
  194.             case openItem:
  195.                 doOpenItem () ;
  196.                  break ;
  197.             case saveAsItem:
  198.                 doSaveAsItem () ;
  199.                 break ;
  200.             case flipItem:
  201.                 doFlipItem () ;
  202.                 break ;
  203.             case someOffItem:
  204.                 doSomeOffItem (onOffList, oolSize) ;
  205.                 break ;
  206.             case someOnItem:
  207.                 doSomeOnItem (onOffList, oolSize) ;
  208.                 break ;
  209.             case copyrightItem:
  210.                 doCopyrightItem () ;
  211.                 break ;
  212.             default:
  213.                 break ;
  214.             }
  215.         
  216.         /* remove local constants */
  217.         #undef        oolSize
  218.         }
  219.  
  220.  
  221. /*--------------------------------- doOrwellItem ------------------------------*/
  222.  
  223. /* deal with a click of the orwellItem  button */
  224.  
  225. void    doOrwellItem () 
  226.         
  227.         {
  228.         /* local constants */
  229.         #define            cyclesDesired        4
  230.         #define            delayTicksOne        20
  231.         #define            delayTicksTwo        10
  232.         
  233.         /* local variables */
  234.         Rect                scratch ;
  235.         ControlHandle    theItemHandle ;
  236.         short                cycleCounter ;
  237.         ControlHandle    ronItemHandle ;
  238.         
  239.         /* get a handle to the button */
  240.         GetDItem ( ourDialog, orwellItem, &scratch, &theItemHandle, &scratch) ;
  241.         
  242.         /* hilite the button */
  243.         HiliteControl (theItemHandle, hilitedHS ) ;
  244.         
  245.         /* get a handle to the ronItem button */
  246.         GetDItem ( ourDialog, ronItem, &scratch, &ronItemHandle, &scratch) ;
  247.  
  248.         /* run several fade cycles on the ronItem button  */
  249.         for ( cycleCounter = 0; cycleCounter < cyclesDesired; cycleCounter++)
  250.             {
  251.             /* fade out */
  252.             HiliteControl (ronItemHandle, inactiveHS ) ;
  253.  
  254.             /* wait a while */
  255.             Delay (delayTicksOne, &scratch) ;
  256.             
  257.             /* back into view */
  258.             HiliteControl (ronItemHandle, activeHS ) ;
  259.  
  260.             /* wait a while */
  261.             Delay (delayTicksTwo, &scratch) ;
  262.             }
  263.  
  264.         /* unhilite the button */
  265.         HiliteControl (theItemHandle, activeHS ) ;
  266.  
  267.         /* remove local constants */
  268.         #undef        cyclesDesired
  269.         #undef        delayTicksOne
  270.         #undef        delayTicksTwo    
  271.         }
  272.  
  273.         
  274. /*--------------------------------- doSnapshotItem ----------------------------*/
  275.  
  276. /* deal with a click of the snapshotItem button */
  277.  
  278. void    doSnapshotItem () 
  279.         
  280.         {
  281.         /* local variables */
  282.         ControlHandle    theItemHandle ;
  283.         Rect                scratch ;
  284.         GrafPtr            entryGrafPort ;
  285.         
  286.         /* get a handle to the button */
  287.         GetDItem ( ourDialog, snapshotItem, &scratch, &theItemHandle, &scratch) ;
  288.         
  289.         /* hilite the button */
  290.         HiliteControl (theItemHandle, hilitedHS ) ;
  291.     
  292.         /* save a pointer to the grafPort */
  293.         GetPort(&entryGrafPort) ;            
  294.  
  295.         /* this lets me take some snapshots */
  296.         /* has no effect unless you have a desk accessory named Camera */
  297.         OpenDeskAcc ("\007\000Camera") ;
  298.         
  299.         /* restore the grafPort */
  300.         SetPort(entryGrafPort) ;
  301.         
  302.         /* unhilite the button */
  303.         HiliteControl (theItemHandle, activeHS ) ;
  304.         }
  305.         
  306.         
  307. /*--------------------------------- doMushroomItem ----------------------------*/
  308.  
  309. /* deal with a click of the mushroomItem  button */
  310.  
  311. void    doMushroomItem () 
  312.         
  313.         {
  314.         /* local constants */
  315.         #define            cyclesDesired        4
  316.         #define            delayTicks            30
  317.         
  318.         /* local variables */
  319.         Rect                scratch ;
  320.         short                cycleCounter ;
  321.         ControlHandle    itemHandleOne ;
  322.         ControlHandle    itemHandleTwo ;
  323.         
  324.         /* get a handle to the button */
  325.         GetDItem ( ourDialog, mushroomItem, &scratch, &itemHandleOne, &scratch) ;
  326.         
  327.         /* hilite the button */
  328.         HiliteControl (itemHandleOne, hilitedHS ) ;
  329.         
  330.         /* get a handle to the bumperStickersItem button */
  331.         GetDItem ( ourDialog, bumperStickersItem, &scratch, 
  332.                         &itemHandleTwo, &scratch) ;
  333.  
  334.         /* run several fade cycles on the bumperStickersItem button  */
  335.         for ( cycleCounter = 0; cycleCounter < cyclesDesired; cycleCounter++)
  336.             {
  337.             /* fade out */
  338.             HiliteControl (itemHandleTwo, hilitedHS ) ;
  339.  
  340.             /* wait a while */
  341.             Delay (delayTicks, &scratch) ;
  342.             
  343.             /* back into view */
  344.             HiliteControl (itemHandleTwo, activeHS ) ;
  345.  
  346.             /* wait a while */
  347.             Delay (delayTicks, &scratch) ;
  348.             }
  349.  
  350.         /* unhilite the button */
  351.         HiliteControl (itemHandleOne, activeHS ) ;
  352.  
  353.         /* remove local constants */
  354.         #undef        cyclesDesired
  355.         #undef        delayTicks
  356.         }
  357.  
  358.         
  359. /*----------------------------------- doOpenItem ------------------------------*/
  360.  
  361. /* deal with a click of the openItem button */
  362.  
  363. void    doOpenItem () 
  364.  
  365.         {
  366.         /* local variables */
  367.         Rect                scratch ;
  368.         ControlHandle    theItemHandle ;
  369.         DialogTHndl        theDLOGHandle ;
  370.         SFReply            dummyReply ;
  371.         
  372.         /* get a handle to the button */
  373.         GetDItem ( ourDialog, openItem, &scratch, &theItemHandle, &scratch) ;
  374.         
  375.         /* hilite the button */
  376.         HiliteControl (theItemHandle, hilitedHS ) ;
  377.         
  378.         /* run the standard file open dialog */
  379.         theDLOGHandle = (DialogTHndl) GetResource ('DLOG', getDlgID) ;
  380.         SFGetFile (figureCenteredRectTLC (&(**theDLOGHandle).boundsRect),
  381.                          nil, nil, allTypes, nil, nil, &dummyReply ) ;
  382.         
  383.         /* unhilite the button */
  384.         HiliteControl (theItemHandle, activeHS ) ;
  385.         }
  386.         
  387.  
  388. /*-------------------------------- doSaveAsItem -------------------------------*/
  389.  
  390. /* deal with a click of the saveAsItem button */
  391.  
  392. void    doSaveAsItem () 
  393.  
  394.         {
  395.         /* local variables */
  396.         Rect                scratch ;
  397.         ControlHandle    theItemHandle ;
  398.         DialogTHndl        theDLOGHandle ;
  399.         SFReply            dummyReply ;
  400.  
  401.         /* get a handle to the button */
  402.         GetDItem ( ourDialog, saveAsItem, &scratch, &theItemHandle, &scratch) ;
  403.         
  404.         /* hilite the button */
  405.         HiliteControl (theItemHandle, hilitedHS ) ;
  406.         
  407.         /* run the standard file open dialog */
  408.         theDLOGHandle = (DialogTHndl) GetResource ('DLOG', putDlgID) ;
  409.         SFPutFile (figureCenteredRectTLC (&(**theDLOGHandle).boundsRect),
  410.                         "\015Save file as:", "\021Current File Name", 
  411.                         nil, &dummyReply ) ;
  412.  
  413.         /* unhilite the button */
  414.         HiliteControl (theItemHandle, activeHS ) ;
  415.         }
  416.  
  417.  
  418. /*--------------------------------- doFlipItem --------------------------------*/
  419.  
  420. /* deal with a click of the flipItem button */
  421.  
  422. void    doFlipItem () 
  423.  
  424.         {
  425.         /* local constants */
  426.         #define        numButtons        9
  427.         #define        cyclesDesired    4
  428.         #define        delayTicks        15
  429.         
  430.         /* local variables */
  431.         static    short    flipList [numButtons] =    {     hupCoupleItem,     mouthOpensItem, 
  432.                                                                 trashItem,             melancholyItem, 
  433.                                                                 eeekShrinkItem,     mushroomItem, 
  434.                                                                 duplicateItem,     orwellItem, 
  435.                                                                 bumperStickersItem                          } ; 
  436.         Rect                scratch ;
  437.         ControlHandle    theItemHandle ;
  438.         short                cycleCounter ;
  439.         short                index ;
  440.         ControlHandle    tempItemHandle ;
  441.         
  442.         /* get a handle to the button */
  443.         GetDItem ( ourDialog, flipItem, &scratch, &theItemHandle, &scratch) ;
  444.         
  445.         /* hilite the button */
  446.         HiliteControl (theItemHandle, hilitedHS ) ;
  447.         
  448.         /* run several animation cycles on a group of content-changing buttons  */
  449.         for ( cycleCounter = 0; cycleCounter < cyclesDesired; cycleCounter++)
  450.             {
  451.             /* hilite all the buttons in the group  */
  452.             for (index = 0 ; index < numButtons ; index++)
  453.                 {
  454.                 GetDItem ( ourDialog, flipList[index], &scratch, 
  455.                             &tempItemHandle, &scratch) ;
  456.                 HiliteControl (tempItemHandle, hilitedHS ) ;
  457.                 }
  458.             
  459.             /* wait a while */
  460.             Delay (delayTicks, &scratch) ;
  461.             
  462.             /* unhilite all the buttons in the group */
  463.             for (index = 0 ; index < numButtons ; index++)
  464.                 {
  465.                 GetDItem ( ourDialog, flipList[index], &scratch, 
  466.                             &tempItemHandle, &scratch) ;
  467.                 HiliteControl (tempItemHandle, activeHS ) ;
  468.                 }
  469.  
  470.             /* wait a while */
  471.             Delay (delayTicks, &scratch) ;
  472.             }
  473.  
  474.         /* unhilite the button */
  475.         HiliteControl (theItemHandle, activeHS ) ;
  476.  
  477.         /* remove local constants */
  478.         #undef        numButtons
  479.         #undef        cyclesDesired
  480.         #undef        delayTicks    
  481.         }
  482.     
  483.     
  484. /*------------------------------- doSomeOffItem -------------------------------*/
  485.  
  486. /* deal with a click of the someOffItem button  */
  487.  
  488. void    doSomeOffItem (theOffList, listSize) 
  489.         short        theOffList[] ;
  490.         short        listSize ;
  491.         
  492.         {
  493.         /* local variables */
  494.         short                index ;
  495.         Rect                scratch ;
  496.         ControlHandle    theItemHandle ;
  497.         ControlHandle    tempItemHandle ;
  498.         
  499.         /* get a handle to the button  */
  500.         GetDItem ( ourDialog, someOffItem, &scratch, &theItemHandle, &scratch) ;
  501.         
  502.         /* hilite the button */
  503.         HiliteControl (theItemHandle, hilitedHS ) ;
  504.  
  505.         /* for each item in the list */
  506.         for (index = 0 ; index < listSize ; index++)
  507.             {
  508.             /* get a handle to the item */
  509.             GetDItem ( ourDialog, theOffList[index], &scratch, 
  510.                         &tempItemHandle, &scratch) ;
  511.             
  512.             /* inactivate the item */
  513.             HiliteControl (tempItemHandle, inactiveHS ) ;
  514.             }
  515.         
  516.         /* unhilite the someOffItem button  */
  517.         HiliteControl (theItemHandle, activeHS ) ;
  518.         }
  519.  
  520.  
  521. /*-------------------------------- doSomeOnItem -------------------------------*/
  522.  
  523. /* deal with a hit of the someOnItem button  */
  524.  
  525. void    doSomeOnItem (theOnList, listSize) 
  526.         short        theOnList[] ;
  527.         short        listSize ;
  528.         
  529.         {
  530.         /* local variables */
  531.         short                index ;
  532.         Rect                scratch ;
  533.         ControlHandle    theItemHandle ;
  534.         ControlHandle    tempItemHandle ;
  535.         
  536.         /* get a handle to the button  */
  537.         GetDItem ( ourDialog, someOnItem, &scratch, &theItemHandle, &scratch) ;
  538.         
  539.         /* hilite the button */
  540.         HiliteControl (theItemHandle, hilitedHS ) ;
  541.  
  542.         /* for each item in the list */
  543.         for (index = 0 ; index < listSize ; index++)
  544.             {
  545.             /* get a handle to the item */
  546.             GetDItem ( ourDialog, theOnList[index], &scratch, 
  547.                         &tempItemHandle, &scratch) ;
  548.             
  549.             /* activate the item */
  550.             HiliteControl (tempItemHandle, activeHS ) ;
  551.             }
  552.  
  553.         /* unhilite the someOnItem button  */
  554.         HiliteControl (theItemHandle, activeHS ) ;
  555.         }
  556.  
  557.  
  558. /*--------------------------------doCopyrightItem -----------------------------*/
  559.  
  560. /* deal with a hit on the copyrightItem button */
  561.  
  562. void    doCopyrightItem ()
  563.  
  564.         {
  565.         /* local variables */
  566.         Rect                scratch ;
  567.         ControlHandle    theItemHandle ;
  568.         DialogPtr        copyrightDlog ;
  569.         Point                tempPoint ;
  570.         
  571.         /* get a handle to the button */
  572.         GetDItem ( ourDialog, copyrightItem, &scratch, &theItemHandle, &scratch) ;
  573.         
  574.         /* hilite the button */
  575.         HiliteControl (theItemHandle, hilitedHS ) ;
  576.         
  577.         /* pull in the copyright notice modal dialog */
  578.         copyrightDlog = GetNewDialog (copyrightDlogID, storeInHeap, inFront) ;
  579.  
  580.         /* center it on the screen */
  581.         MoveWindow ( copyrightDlog, 
  582.                  (tempPoint = figureCenteredRectTLC (&(*copyrightDlog).portRect)).h,
  583.                     tempPoint.v, inFront ) ;
  584.         
  585.         /* show the copyright notice */
  586.         ShowWindow (copyrightDlog ) ;
  587.     
  588.         /* wait until the user clicks the mouse in the dialog */
  589.         ModalDialog (noFilterProcedure, &scratch) ;
  590.         
  591.         /* get rid of the dialog */
  592.         DisposDialog (copyrightDlog) ;
  593.  
  594.         /* unhilite the button */
  595.         HiliteControl (theItemHandle, activeHS ) ;
  596.         }
  597.         
  598.         
  599. /*--------------------------- figureCenteredRectTLC ---------------------------*/
  600.  
  601. /* given a rectangle, returns the top left corner  position that will 
  602.     center the rectangle inside screen area that's below the menu bar */
  603.  
  604. Point    figureCenteredRectTLC (theRect)
  605.         Rect    *theRect ;
  606.  
  607.         {
  608.          /* local variable */
  609.         Point    theResult ;
  610.          
  611.          /* figure the vertical position */
  612.          theResult.v = menuBarHeight + ((screenHeight - menuBarHeight) - 
  613.                                          (theRect->bottom - theRect->top) )  /  2 ;
  614.              
  615.          /* figure the horizontal position */
  616.          theResult.h = ( screenWidth - (theRect->right - theRect->left) )   /   2 ;
  617.          
  618.          /* done, so return the point */
  619.          return (theResult) ;
  620.         }
  621.